//app.js
// 1: 参会人员 2: 代理商 3: 代理商助理 4: 扫码人员
const desCode = require('/utils/aesUtil.js');
const mdCode = require('/utils/md5.js');
const api_js = require("/utils/api.js");
const util_js = require("/utils/util.js");
var app = getApp();

var UserID = wx.getStorageSync("UserID") || '';
var UserName = '';
var Phone = '';

App({
  onLaunch: function () {

    wx.getSystemInfo({
      success: (res) => {
        console.log(res.model, "手机型号手机型号手机型号手机型号手机型号手机型号手机型号");
        if (-1 != res.model.search("iPhone X") || -1 != res.model.search("iPhone XS") || -1 != res.model.search("iPhone XR") || -1 != res.model.search("iPhone XS Max") || -1 != res.model.search("iPhone 11") || -1 != res.model.search("iPhone 12")) {
          this.globalData.tabBar.isIphoneX = true;
          this.globalData.tabBar2.isIphoneX = true;
          this.globalData.tabBar3.isIphoneX = true;
          this.globalData.isIphoneX = true;
    

        } else {
          this.globalData.tabBar.isIphoneX = false;
          this.globalData.tabBar2.isIphoneX = false;
          this.globalData.tabBar3.isIphoneX = false;
          this.globalData.isIphoneX = false;
        }
      }
    })

    this.globalData.UserID = wx.getStorageSync("UserID") || '';

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var openid;
        var userid;
        var code = res.code;
        console.log(res, "登录的resresresres");
        console.log("小程序code " + code)
        var wMsgID = desCode.wMsgID();
        var wParam = desCode.to3des('code=' + code);
        var md = mdCode.hexMD5('9999' + '105' + wMsgID + wParam + 'q1w2e3r4t5y6');
        let data = {
          wAgent: 9999,
          wAction: 105,
          wMsgID: wMsgID,
          wParam: wParam,
          wSign: md,
          wImei: 222,
          wVersion: 2,
          wRequestUserID: 4
        }
        api_js.postReq(data, (res) => {
          console.log(res, "获取openid")

          // return;
          if (res.ReturnCode == 0) {
            var openid = res.Data[0].openid.replace(/_/g, "^=^")
            // var openid = "oGCIr43rhGgJFqA7MOqVhIb0FU$c"
            // console.log('自动登录 UserID =', UserID);
            var that = this;
            that.globalData.openid = openid;
            console.log('获取到的 openid =', openid);
            // 获取登录状态
            var wMsgID = desCode.wMsgID();
            var wParam = desCode.to3des('openid=' + openid);
            var md = mdCode.hexMD5('9999' + '101' + wMsgID + wParam + 'q1w2e3r4t5y6');
            let data = {
              wAgent: 9999,
              wAction: 101,
              wMsgID: wMsgID,
              wParam: wParam,
              wSign: md,
              wImei: 222,
              wVersion: 2,
              wRequestUserID: 4
            }
            api_js.postReq(data, (res) => {
              console.log(res, "登录状态");
              // 登录状态 0：登录 1：过期
              // 没有注册的情况
              if (res.ReturnCode == 110) {
                this.globalData.LoginStatus = 1;
                console.log(this.globalData.LoginStatus, "this.globalData.LoginStatus")
              };
              if (res.Data) { 
                if (res.Data[0].loginstatus == 0) {
                  this.globalData.LoginStatus = 0;
                  this.globalData.UserID = res.Data[0].userid;
                  this.globalData.Phone = res.Data[0].phone;
                  this.globalData.UserName = res.Data[0].nlikename;
                  this.globalData.HeadPortrait = res.Data[0].headportrait;
                  this.globalData.Status = res.Data[0].status;
                  // this.globalData.Status = 2;

                  wx.setStorageSync("UserID", UserID);
                  if (this.globalData.Status == 2) {
                    wx.reLaunch({
                      url: '../../pages/scanPeople/scanPeople'
                    });
                  } else {
                    wx.reLaunch({
                      url: '../../pages/index/index'
                    })
                  }
                } else {
                  this.globalData.LoginStatus = 1;
                }
              }

            })
          }
        })
      }
    })






    // 获取用户信息
    wx.getSetting({
      success: res => {
        console.log('wx.getSetting');
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  //第一种状态的底部
  editTabBar: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (i == 0) {

        if (tabBar.list[i].pagePath == _pagePath + '?UserName=' + this.globalData.UserName
          + '&UserID=' + this.globalData.UserID + '&Phone=' + this.globalData.Phone) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态  
        }
      }
      else {
        if (tabBar.list[i].pagePath == _pagePath) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态  
        }
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  editTabBar2: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabBar2;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == _pagePath) {
        tabBar.list[i].active = true;//根据页面地址设置当前页面状态  
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  editTabBar3: function () {
    var that = this;
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = that.globalData.tabBar3;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (i == 0) {
        if (tabBar.list[i].pagePath == _pagePath + '?UserName=' + that.globalData.UserName
          + '&UserID=' + that.globalData.UserID + '&Phone=' + that.globalData.Phone) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态
        }
      } else {
        if (tabBar.list[i].pagePath == _pagePath) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态  
        }
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {


  },
  updateglobalData: function (data) {

  },

  globalData: {
    userInfo: null,
    UserName: '',
    UserID: '',
    Phone: '',
    // 身份 1:参会人员 2:扫码人员
    Status: 1,
    HeadPortrait: '',
    isLogin: false,
    isIphoneX: false,
    // 是否登录转态(默认没登录)
    LoginStatus: 1,
    tabBar: {
      "color": "#8f9198",
      "selectedColor": "#9c528a",
      "backgroundColor": "#fff",
      "borderStyle": "#eee",
      "list": [
        {
          "pagePath": "/pages/index/index?UserName=",
          "text": "首页",
          "iconPath": "/images/link-icon1.png",
          "selectedIconPath": "/images/link-icon1-hover.png",
          "clas": "menu-item",
          "active": true,
        },
        {
          "pagePath": "/pages/team/team",
          "text": "团队预约",
          "iconPath": "/images/TeamUnSelected.png",
          "selectedIconPath": "/images/Team.png",
          "clas": "menu-item",
          "active": false,
        },
        {
          "pagePath": "/pages/my/my",
          "text": "我的",
          "iconPath": "/images/link-icon2.png",
          "selectedIconPath": "/images/link-icon2-hover.png",
          "clas": "menu-item",
          "active": false,
        }
      ],
      "position": "bottom",
      "isIphoneX": false
    },


    tabBar2: {
      "color": "#8f9198",
      "selectedColor": "#9c528a",
      "backgroundColor": "#fff",
      "borderStyle": "#eee",
      "list": [
        {
          "pagePath": "/pages/scanPeople/scanPeople",
          "text": "首页",
          "iconPath": "/images/link-icon1.png",
          "selectedIconPath": "/images/link-icon1-hover.png",
          "clas": "menu-item",
          "active": true
        },
        {
          "pagePath": "/pages/my/my",
          "text": "我的",
          "iconPath": "/images/link-icon2.png",
          "selectedIconPath": "/images/link-icon2-hover.png",
          "clas": "menu-item",
          "active": false
        }
      ],
      "position": "bottom",
      "isIphoneX": false

    },

    // 普通用户
    tabBar3: {
      "color": "#8f9198",
      "selectedColor": "#9c528a",
      "backgroundColor": "#fff",
      "borderStyle": "#eee",
      "list": [
        {
          "pagePath": "/pages/index/index?UserName=",
          "text": "首页",
          "iconPath": "/images/link-icon1.png",
          "selectedIconPath": "/images/link-icon1-hover.png",
          "clas": "menu-item",
          "active": true
        },
        {
          "pagePath": "/pages/my/my",
          "text": "我的",
          "iconPath": "/images/link-icon2.png",
          "selectedIconPath": "/images/link-icon2-hover.png",
          "clas": "menu-item",
          "active": false
        }
      ],
      "position": "bottom",
      "isIphoneX": false
    },
  },


})